package com.milotnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milotnt.mapper.MemberMapper;
import com.milotnt.pojo.ClassOrder;
import com.milotnt.pojo.Member;
import com.milotnt.service.ClassOrderService;
import com.milotnt.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhangMing [1157038410@qq.com]
 * @date 2021/8/11
 */

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private ClassOrderService orderService;

    @Override
    public List<Member> findAll() {
        return this.list();
    }

    @Override
    public Boolean insertMember(Member member) {
        return this.save(member);
    }

    @Override
    public Boolean updateMemberByMemberAccount(Member member) {
        return this.updateById(member);
    }

    @Override
    public Member userLogin(Member member) {
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Member::getMemberAccount, member.getMemberAccount());
        lqw.eq(Member::getMemberPassword, member.getMemberPassword());
        return this.getOne(lqw);
    }

    @Override
    public Boolean deleteByMemberAccount(Integer memberAccount) {
        // 删除当前会员的课程信息
        LambdaQueryWrapper<ClassOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassOrder::getMemberAccount, memberAccount);
        orderService.remove(lqw);
        return this.removeById(memberAccount);
    }

    @Override
    public Integer selectTotalCount() {
        return Math.toIntExact(this.count());
    }

    @Override
    public List<Member> selectByMemberAccount(Integer memberAccount) {
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Member::getMemberAccount, memberAccount);
        return this.list(lqw);
    }

    @Override
    public Member updateCardClass(Integer memberAccount, int time) {
        Member member = this.getById(memberAccount);
        member.setCardClass(member.getCardClass() - time);
        this.updateById(member);
        return member;
    }
}
